package weifei.wx.community.controller;


import com.auth0.jwt.JWT;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import weifei.web.Result;
import weifei.wx.community.entity.dto.TieCommentReplyNoticeDTO;
import weifei.wx.community.mapper.TieCommentReplyNoticeMapper;
import weifei.wx.community.service.TieCommentReplyService;
import weifei.wx.community.service.TieCommentService;

@RestController
@RequestMapping("/tie/comment/reply/notice")
public class TieCommentReplyNoticeController
{
    public static final int PAGE_COUNT = 10;


    @Autowired
    private TieCommentService tieCommentService;
    @Autowired
    private TieCommentReplyService tieCommentReplyService;

    @Autowired
    private TieCommentReplyNoticeMapper tieCommentReplyNoticeMapper;


    @RequestMapping("/getNoticeByPage")
    public Result getNoticeByPage(
            HttpServletRequest request,
            @Valid @NotNull Integer page
            )
    {
        String token = request.getHeader("access_token");
        var uid = JWT.decode(token).getClaim("user_id").asLong();

        QueryWrapper queryWrapper = QueryWrapper.create()
                .select()
                .where("tie_comment_user_id = ?", uid)
                .orderBy("tie_comment_reply_id desc")
                .limit(PAGE_COUNT * (page - 1), PAGE_COUNT);
        var list = this.tieCommentReplyNoticeMapper.selectListByQuery(queryWrapper);
        var dtoArray = new TieCommentReplyNoticeDTO[list.size()];

        for (int i = 0; i < list.size(); i++)
        {
            final var notice = list.get(i);
            final var replyDTO = this.tieCommentReplyService.getTieCommentReplyByID(notice.getTieCommentReplyId());
            final var commentDTO = this.tieCommentService.getTieCommentByID(replyDTO.getTieCommentId());
            dtoArray[i] = new TieCommentReplyNoticeDTO(notice, commentDTO, replyDTO);
        }
        return Result.success("success", dtoArray);
    }
}



